'''
链表排序：
先转换成列表，排序后，再转换成链表。
性能比较低，但是简单。
'''
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def sortList(head: ListNode) -> ListNode:
    # 将链表转换为列表
    values = []
    current = head
    while current:
        values.append(current.val)
        current = current.next

    # 对列表进行排序
    values.sort()

    # 将列表转换回链表
    dummy = ListNode()
    current = dummy
    for value in values:
        current.next = ListNode(value)
        current = current.next

    return dummy.next

# 辅助函数：创建链表
def createList(values):
    if not values:
        return None
    head = ListNode(values[0])
    current = head
    for value in values[1:]:
        current.next = ListNode(value)
        current = current.next
    return head

# 辅助函数：打印链表
def printList(node: ListNode):
    while node:
        print(node.val, end=" -> ")
        node = node.next
    print("None")

# 测试用例
def test_sortList():
    # 测试用例 1
    head1 = createList([4, 2, 1, 3])
    result1 = sortList(head1)
    print("Test case 1:")
    printList(result1)  # 预期输出: 1 -> 2 -> 3 -> 4 -> None

    # 测试用例 2
    head2 = createList([-1, 5, 3, 4, 0])
    result2 = sortList(head2)
    print("Test case 2:")
    printList(result2)  # 预期输出: -1 -> 0 -> 3 -> 4 -> 5 -> None

    # 测试用例 3
    head3 = createList([])
    result3 = sortList(head3)
    print("Test case 3:")
    printList(result3)  # 预期输出: None

    # 测试用例 4
    head4 = createList([1])
    result4 = sortList(head4)
    print("Test case 4:")
    printList(result4)  # 预期输出: 1 -> None

    # 测试用例 5
    head5 = createList([5, -3, 2, 8, 0])
    result5 = sortList(head5)
    print("Test case 5:")
    printList(result5)  # 预期输出: -3 -> 0 -> 2 -> 5 -> 8 -> None

# 运行测试用例
test_sortList()
